﻿// Decompiled with JetBrains decompiler
// Type: UnityEngine.NoAllocHelpers
// Assembly: UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E6AE6B5A-2752-4B07-9528-C8B77C62CE79
// Assembly location: C:\Users\windys\Unity\2021.3.39f1c1\Editor\Data\Managed\UnityEngine\UnityEngine.CoreModule.dll
// XML documentation location: C:\Users\windys\Unity\2021.3.39f1c1\Editor\Data\Managed\UnityEngine\UnityEngine.CoreModule.xml

using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine.Bindings;

#nullable disable
namespace UnityEngine
{
  [NativeHeader("Runtime/Export/Scripting/NoAllocHelpers.bindings.h")]
  internal sealed class NoAllocHelpers
  {
    public static void ResizeList<T>(List<T> list, int size)
    {
      if (list == null)
        throw new ArgumentNullException(nameof (list));
      if (size < 0 || size > list.Capacity)
        throw new ArgumentException("invalid size to resize.", nameof (list));
      if (size == list.Count)
        return;
      NoAllocHelpers.Internal_ResizeList((object) list, size);
    }

    public static void EnsureListElemCount<T>(List<T> list, int count)
    {
      list.Clear();
      if (list.Capacity < count)
        list.Capacity = count;
      NoAllocHelpers.ResizeList<T>(list, count);
    }

    public static int SafeLength(Array values) => values != null ? values.Length : 0;

    public static int SafeLength<T>(List<T> values) => values != null ? values.Count : 0;

    public static T[] ExtractArrayFromListT<T>(List<T> list)
    {
      return (T[]) NoAllocHelpers.ExtractArrayFromList((object) list);
    }

    [FreeFunction("NoAllocHelpers_Bindings::Internal_ResizeList")]
    [MethodImpl(MethodImplOptions.InternalCall)]
    internal static extern void Internal_ResizeList(object list, int size);

    [FreeFunction("NoAllocHelpers_Bindings::ExtractArrayFromList")]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern Array ExtractArrayFromList(object list);
  }
}
